package com.fitbet.wechat.web.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.fitbet.entity.bet.Bet;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.user.User;
import com.fitbet.wechat.processor.WechatProcessor;
import com.fitbet.wechat.service.BetService;
import com.fitbet.wechat.service.UserBetService;
import com.fitbet.wechat.web.interceptor.UserContext;
import com.fitbet.wechat.web.interceptor.UserContextType;
import com.fitbet.wechat.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("")
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
    @Autowired
    private BetService betService;
    
    @Autowired
    private UserBetService userBetService;
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    /**
     * 
      * 堵我会瘦第一页
     * 
     * @return
     */
    @RequestMapping("/welcome")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView welcome(@Secure User user, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("user", user);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        logger.info("get user [{}]", JsonUtils.toJson(user));
        
        return modelAndView;
    }
    
    /**
     *  赌瘦第一页，规则活动介绍页,准备页
     * 
     * @return
     */
    @RequestMapping("/index")
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView index(@Secure User user, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("user", user);
        
        // 当前可报名的期数
        Bet bet = betService.loadCurrentBet(new Date());
        modelAndView.addObject("bet", bet);
        
        UserBet userBet = userBetService.loadUserBet(user.getId(), bet.getId(), new UserBetStatus[]{UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE,  UserBetStatus.CLOSEFAILED, UserBetStatus.PROCESSING, UserBetStatus.FINISHED});
        if(userBet != null) {
            modelAndView.setViewName("redirect:/profile/bets/" + bet.getId());
            return modelAndView;
        }
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * mall index
     * 
     * @return
     */
    @RequestMapping("/mall")
    public ModelAndView mall(@Secure User user, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("mall");
        
        modelAndView.addObject("user", user);
        
        Bet bet = betService.loadCurrentBet(new Date());
        modelAndView.addObject("bet", bet);
        
        List<UserBet> hotUserBets = userBetService.listHotUserBet(new UserBetStatus[]{UserBetStatus.PROCESSING, UserBetStatus.PAID, UserBetStatus.OPEN, UserBetStatus.OPENFAILED, UserBetStatus.CLOSE});
        modelAndView.addObject("hotUserBets", hotUserBets);
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping("/weixin-required")
    public String weixin() {
        return "wechat-required";
    }
}
